package buffer;

import org.junit.Test;

// buffer缓存: StringBuilder带缓存的字符串
public class StringBuilderDemo {
	
	@Test
	public void create() {
		//底层是由一个可变 char[] toStringCache;
		// 1.无参构造. new char[16];
		StringBuilder buffer = new StringBuilder();
		// 2.由字符串构造
		StringBuilder b1 = new StringBuilder("hello");
		// 3. new char[32];
		StringBuilder b2 = new StringBuilder(32);
	}
	
	@Test
	public void useful() {
		//1.追加    new char[16];
		StringBuilder b = new StringBuilder();
		b.append("abc");  // "abc" 3
		b.append("abcde");   // 
		b.append("aaaaa");  // 
		b.append("bbbbb"); //   底层扩容    (已有的个数+要添加的个数) << 1 + 2
		System.out.println(b);
		System.out.println(b.length());   //18
		// 2.删除
		b.deleteCharAt(0);  //bcabcdeaaaaabbbbb
		System.out.println(b);
		b.delete(0, 5);     //deaaaaabbbbb
		System.out.println(b);
		// 3.替换
		b.replace(0,4,"hello");   // helloaaabbbbb
		System.out.println(b);
		// 4.插入
		b.insert(0, true);
		System.out.println(b);
		// 5.逆序
		b.reverse();
		System.out.println(b);
		//6.转成String
		String string = b.toString();
		// 7.设置长度
		b.setLength(5);
		System.out.println(b);
	}
	
}
